define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'ykjp/purchase/ret_review/index' + location.search,
                    add_url: 'ykjp/purchase/retire/add',
                    edit_url: 'ykjp/purchase/retire/edit',
                    detail_url: "ykjp/purchase/ret_review/detail",
                    del_url: 'ykjp/purchase/retire/del',
                    approval_url: "ykjp/purchase/ret_review/reviewRet?type=",
                    table: 'ykjp_retire',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true, formatter: function (value, row, index) {
                                if (row.status == 0) {
                                    return {
                                        disabled: false,
                                    };
                                } else {
                                    return {
                                        disabled: true,
                                    }

                                }
                            }},
                        {field: 'code', title: __('Code')},
                        {field: 'supplier.name', title: __('Supplier.name')},
                        {field: 'billing_time', title: __('Billing_time'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'remark', title: __('Remark')},
                        {field: 'status', title: __('Status'), searchList: {"0": __('Status 0'), "1": __('Status 1'), "2": __('Status 2'), "3": __('Status 3'), "4": __('Status 4')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'detail',
                                    title: __('look'),
                                    classname: 'btn btn-xs btn-primary  btn-addtabs',
                                    icon: 'fa fa-pencil-square-o',
                                    url: $.fn.bootstrapTable.defaults.extend.detail_url,
                                },
                                {
                                    name: 'yes',
                                    title: __('Approved yes'),
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    icon: 'fa fa-leaf',
                                    url: $.fn.bootstrapTable.defaults.extend.approval_url + 'yes',
                                    hidden: function (row) {
                                        if (row.status == 0) {
                                            return false;
                                        } else {
                                            return true;
                                        }
                                    },
                                    success: function (data, ret) {
                                        //如果需要阻止成功提示，则必须使用return false;
                                        //return false;
                                        layer.msg('提交成功', {
                                            time: 2000, //2s后自动关闭
                                            end: function () {
                                                location.reload();
                                            }
                                        })
                                    },
                                    error: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                },

                                {
                                    name: 'no',
                                    title: __('Approved no'),
                                    classname: 'btn btn-xs btn-info btn-click',
                                    icon: 'fa fa-exclamation-triangle',
                                    hidden: function (row) {
                                        if (row.status == 0) {
                                            return false;
                                        } else {
                                            return true;
                                        }
                                    },
                                    click: function (data, row) {
                                        appWhy(row.id);
                                    }
                                },
                            ],
                            formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                if (row.status == 3 || row.status == 4) {
                                    $(table).data("operate-del", null);
                                }
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        }
                    ]
                ]
            });

            $(document).on("click", ".btn-selected", function () {
                var checked = table.bootstrapTable('getSelections');

                var arr = new Array();
                for (var i = 0; i < checked.length; i++) {
                    arr.push(checked[i].id);
                }
                Fast.api.ajax({
                    url: $.fn.bootstrapTable.defaults.extend.approval_url + "yes",
                    data: {"ids": arr}
                }, function (data, ret) {
                    //成功的回调

                    if (data.result = "success") {

                        layer.msg('成功提交', {
                            time: 2000, //2s后自动关闭
                            end: function () {
                                location.reload();
                            }
                        });
                    }
                }, function (data, ret) {
                    return false;
                });
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
    };
    return Controller;
});

function approval(obj, id) {
    Fast.api.ajax({
        url: "ykjp/purchase/ret_review/reviewRet?ids=" + id,
        data: {"type": obj}
    }, function (data, ret) {
        //成功的回调
        if (data.result = "success") {
            layer.msg('成功提交', {
                time: 2000, //2s后自动关闭
                end: function () {
                    location.reload();
                }
            });
        }
    }, function (data, ret) {
        alert(ret.msg);
        return false;
    });
}

function confirmYes(id) {
    Fast.api.ajax({
        url: "ykjp/purchase/retire/confirm?ids=" + id,
        data: {"type": "yes"}
    }, function (data, ret) {
        //成功的回调
        if (data.result = "success") {
            layer.msg('成功提交', {
                time: 2000, //2s后自动关闭
                end: function () {
                    location.reload();
                }
            });
        }
    }, function (data, ret) {
        return false;
    });
}


function confirmNo(id) {
    var why = $("#why").val();
    Fast.api.ajax({
        url: "ykjp/purchase/retire/confirm?ids=" + id,
        data: {"type": "no", "why": why}
    }, function (data, ret) {
        //成功的回调
        if (data.result = "success") {
            layer.msg('成功提交', {
                time: 2000, //2s后自动关闭
                end: function () {
                    location.reload();
                }
            });
        }
    }, function (data, ret) {
        Layer.alert(ret.msg);
        return false;
    });
}

function Anti(obj) {
    Fast.api.ajax({
        url: "ykjp/purchase/retire/Anti?ids=" + obj,
    }, function (data, ret) {
        //成功的回调
        if (data.result = "success") {
            layer.msg('成功提交', {
                time: 2000, //2s后自动关闭
                end: function () {
                    location.reload();
                }
            });
        }
    }, function (data, ret) {
        alert(ret.msg);
        return false;
    });
}

function appWhy(id) {
    var str = "<div  style='width:80%;margin-left:auto;margin-right:auto'><textarea class='reason' rows='3' cols='38'></textarea></div>";
    Layer.open({
        title: __('Please fill in the reason'), //页面标题
        content: str,
        area: ['400px', '230px'], //自定义文本域宽高
        btn: ['确定', '取消'],
        yes: function (e, value) {
            var reason = $(".reason").val().trim();
            if (reason == "") {
                alert(__("Reason cannot be empty"));
            } else {
                apprNo(reason, id);
            }
        }, btn2: function () {
            return 0;
        }
    })
}


function apprNo(why, id) {
    Fast.api.ajax({
        url: "ykjp/purchase/ret_review/reviewRet?ids=" + id,
        data: {"type": "no", "reason": why}
    }, function (data, ret) {
        //成功的回调

        if (data.result = "success") {

            layer.msg('成功提交', {
                time: 2000, //2s后自动关闭
                end: function () {
                    location.reload();
                }
            });
        }
    }, function (data, ret) {
        Layer.alert(ret.msg);
        return false;
    });
}



